import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {Router} from '@angular/router';
import {TipService} from '../../services/tip.service';
import {ExtendHttpService} from '../../../common/extend-http.service';
import {EssService} from '../ess.service';

@Component({
  selector: 'app-material',
  templateUrl: './material.component.html',
  styleUrls: ['./material.component.css']
})
export class MaterialComponent implements OnInit {

  isLoading = false;
  loading = true;
  isFlag = false;
  tipType = 'error';
  totalPage = 1;
  loadMoreFlag = true;
  list = [];
  params = {start: 1, key: ''};
  isShowSpc: any = true;
  confirmShow: any = false; // 确认框
  deleteId: any = '';
  @Output() viewState = new EventEmitter();

  @Input() orderType: any;

  constructor(private extendHttp: ExtendHttpService,
              private router: Router,
              private tipService: TipService,
              private ess: EssService
  ) {
  }

  ngOnInit() {
    cBack = () => {
      $('#btn-back').trigger('click');
    };

    if (this.orderType) {
      this.isShowSpc = false;
    }

    this.getData();
  }

  getData(searchFlag?: boolean) {
    // if (searchFlag) {
    //   if (!this.params.startTime.trim()) {
    //     this.tipService.change(true, '请选择开始时间');
    //     return;
    //   }
    //   if (!this.params.endTime.trim()) {
    //     this.tipService.change(true, '请选择结束时间');
    //     return;
    //   }
    // }
    this.params.start = this.params.start > 1 ? this.params.start + 1 : 1;
    this.extendHttp.post('/merchantApp/inventory/materiel/findPage', this.params).subscribe(response => {
      let data = response.json();
      if (data.code === '0') {
        this.list = data.page.list;
        this.list.forEach(item => {
          item.unit = JSON.parse(item.unit);
        });
        this.params.start = data.page.pageNumber;
        this.totalPage = data.page.totalPage;
        this.isFlag = true;
        this.loading = false;
      } else {
        this.tipType = 'error';
        this.loading = false;
        this.tipService.change(true, this.ess.errTips(data));
      }
    }, error => {
      this.tipType = 'error';
      this.loading = false;
      this.tipService.change(true, '系统繁忙，操作失败！');
    });
  }

  loadMore() {

    if (this.loadMoreFlag) {
      this.params.start = this.params.start + 1;
      this.loadMoreFlag = false;
      this.extendHttp.post('/merchantApp/inventory/materiel/findPage', this.params).subscribe(response => {
        let data = response.json();
        if (data.code === '0') {
          this.list = this.list.concat(data.page.list);
          this.params.start = data.page.pageNumber;
          this.totalPage = data.page.totalPage;
          this.loadMoreFlag = true;
        } else if (data.code === '-2001') {
          this.tipType = 'error';
          this.tipService.change(true, '登录超时，跳转到登录页面');
          setTimeout(() => {
            this.router.navigate(['/login']);
          }, 2000);
        } else {
          this.params.start = this.params.start - 1;
          this.tipType = 'error';
          this.tipService.change(true, data.message);
          this.loadMoreFlag = true;
        }
      }, error => {
        this.tipType = 'error';
        this.loadMoreFlag = true;
        this.params.start = this.params.start - 1;
        this.tipService.change(true, '系统繁忙，操作失败！');
      });
    }
  }

  search() {
    this.params.start = 1;
    this.list = [];
    this.getData(true);
  }

  clearData() {
    this.params.key = '';
  }

  // 弹框点击确认
  determine() {
    this.confirmShow = false;
    this.delete({});
  }

  // 弹框点击取消
  cancel() {
    this.confirmShow = false;
  }

  edit(v) {
    this.router.navigate(['/main/ess/material/material-add'], {queryParams: {id: v.id}});
  }

  delete(v) {
    this.deleteId = v.id ? v.id : this.deleteId;
    this.confirmShow = true;
    if (v.id) return;

    this.extendHttp.get('/merchantApp/inventory/materiel/delete', {id: this.deleteId}).subscribe(res => {
      if (res.json().code === '0') {
        this.confirmShow = false;
        this.tipType = 'success';
        this.tipService.change(true, res.json().message);
        setTimeout(() => {
          this.getData(true);
        }, 500);
      } else {
        this.confirmShow = false;
        this.tipService.change(true, this.ess.errTips(res.json()));
      }
    });
  }

}
